package com.codeoss.framework.view.pagebean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.codeoss.framework.context.YunContext;
import com.codeoss.framework.data.metadata.Table;
import com.codeoss.framework.data.store.ITableStore;
import com.codeoss.framework.view.clientscript.ButtonParaCallback;
import com.codeoss.framework.view.component.IDataBinding;
import com.codeoss.framework.view.component.IFormFieldExpression;
import com.codeoss.framework.view.component.IVarName;
import com.codeoss.framework.view.component.Page;
import com.codeoss.framework.view.pagebean.base.BasePageBean;
import com.codeoss.framework.view.service.IVariableService;
import com.codeoss.framework.view.util.ComponentDataUtil;
import com.codeoss.framework.view.util.PageBeanUtil;
import com.codeoss.framework.view.util.ValidatorUtil;

@Service
public class ButtonParaPageBean extends BasePageBean {
	public static final String KEY_FORM_FIELD_LIST = "key_form_field_list";
	@Autowired
	private IVariableService variableService;
	@Autowired
	private ValidatorUtil validatorUtil;
	@Autowired
	private ITableStore tableStore;
	@Autowired
	private PageBeanUtil pageBeanUtil;
	@Autowired
	private ComponentDataUtil componentDataUtil;
	public void beforeInit() {
		YunContext yunContext = YunContext.getCurrentContext();
		Page page = yunContext.getPage();
        if(page.isInit()) {
        	return;
        }
        
		List<IDataBinding> formDataBindingList = new ArrayList<IDataBinding>();
		pageBeanUtil.getFormFieldList(page, formDataBindingList);
		page.setAttribute(KEY_FORM_FIELD_LIST, formDataBindingList);
		
		page.setInit(true);
	}
	 
	public void init() {
		YunContext yunContext = YunContext.getCurrentContext();
		Page page = yunContext.getPage();
		List<IDataBinding> formDataBindingList=page.getAttribute(KEY_FORM_FIELD_LIST);
		if(formDataBindingList.size()>0) {
			for(int i=0;i<formDataBindingList.size();i++) {
				IDataBinding dataBinding = formDataBindingList.get(i);
				if(dataBinding instanceof IFormFieldExpression) {
					Object  defaultValue=componentDataUtil.getDefaultValue(yunContext, page, (IFormFieldExpression)dataBinding);
					variableService.setVariableValue(yunContext,((IVarName)dataBinding).getVarName(), defaultValue);
				}else {
					variableService.setVariableValue(yunContext,((IVarName)dataBinding).getVarName(), null);
				}
			}
			pageBeanUtil.generateFormInfo(formDataBindingList);
		}
	}
	
	public void onOk() {
		YunContext yunContext = YunContext.getCurrentContext();
		
		Page page = yunContext.getPage();
		Map<String,Object> paraMap=new HashMap<String,Object>();
		List<IDataBinding> searchDataBindingList =page.getAttribute(KEY_FORM_FIELD_LIST);
		if(searchDataBindingList.size()>0) {
			for (int j = 0; j < searchDataBindingList.size(); j++) {
				IDataBinding dataBinding = searchDataBindingList.get(j);
				Object value = variableService.getVariableValue(yunContext, ((IVarName) dataBinding).getVarName());
				paraMap.put(dataBinding.getDataField(), value);
			}
			Table table=tableStore.get(page.getDataSourceConfigList().get(0).getTableName());
			validatorUtil.validate(yunContext, table, paraMap);
		}
		
		ButtonParaCallback buttonParaCallback=new ButtonParaCallback();
		yunContext.addClientScript(buttonParaCallback);
		yunContext.setRenderCompleted(true);
	}
	
}
